/*
 *
 *  Copyright (C) 2008-2010 GSyC/LibreSoft, Universidad Rey Juan Carlos
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Juan Francisco Gato Luis <jfcogato@gsyc.es>
 *
 */

package com.LibreGeoSocial.App;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.Layers.NodeListLayer;
import com.LibreGeoSocial.App.AR.ARView;
import com.LibreGeoSocial.App.Overlay.MapOverlay;
import com.LibreGeoSocial.App.Routes.LocationService;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class GenericCard extends MapActivity  {

	private static final int MENU_AR = Menu.FIRST + 1;
	private static final int MENU_SHOW_ROUTE = Menu.FIRST + 2;

	private MapView mapView;
	private int iconId;

	protected GeoNode mNode;
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Intent i = getIntent();
		
		if(i.hasExtra("node"))
			mNode = (GeoNode) i.getSerializableExtra("node");
		
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	protected void setIconId(int id){
		iconId = id;
	}
	
	protected void showMap(Double longitude, Double latitude)
    {
    	
        mapView = (MapView) findViewById(R.id.myMapView);
    	 
        MapController mapControl = mapView.getController();
        
        
        GeoPoint geoPoint = new GeoPoint(  (int) (mNode.getLatitude() * 1000000), 
        						  		   (int) (mNode.getLongitude() * 1000000));
        mapControl.setZoom(17);
        mapControl.setCenter(geoPoint);
        
	    //Config overlay
	    MapOverlay myMapOver = new MapOverlay();
	    myMapOver.setDrawable(getResources().getDrawable(iconId));
	    
	    myMapOver.setGeoPoint(geoPoint);
	   
	   
        final List<Overlay> overlays = mapView.getOverlays();
        overlays.add(myMapOver);
	    
	    mapView.setClickable(true);
        
    }
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_AR, 0, "Show in AR")
        		.setIcon(R.drawable.real_view)
                .setAlphabeticShortcut('A');
         
        
        menu.add(0, MENU_SHOW_ROUTE, 0, "Route")
				.setIcon(R.drawable.ruta_negro_48)
		        .setAlphabeticShortcut('B');        
		
        
        return true;
    }
	
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	
    	switch (item.getItemId()) {
    	
    		case MENU_AR:
    			ArrayList<GeoNode> list = new ArrayList<GeoNode>();
    			list.add(mNode);
    			
    			NodeListLayer mylayer = new NodeListLayer("Single node", 0d, "");
    			mylayer.setNodes(list);
    			
    			Intent i = new Intent(this, ARView.class);
    			DataManager.getInstance().putData(DataManager.GENERIC_LAYER, mylayer);
    			DataManager.getInstance().putData(DataManager.LAYER_CATEGORIES, mylayer.getCategories());

                startActivity(i);
    			break;
    			
    		case MENU_SHOW_ROUTE:
    			
    			/* Generate url to google maps
    			 * This urs is opened with browser or google maps application */

    			Location currentLoc = LocationService.getLastKnowLocation();
    			
    			String UrlMap = "http://maps.google.com/maps?f=d&source=s_d&saddr=(" +
    						 	 currentLoc.getLatitude() + ",+" + currentLoc.getLongitude() + 
    			                 ")&daddr=(" + 
    			                 mNode.getLatitude() + ",+" + mNode.getLongitude() + 
    			                 ")&hl=es&geocode=&mra=ls&ie=UTF8&t=h&z=15";
    			
    			Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(UrlMap));
    			startActivity(myIntent);
    			
    			break;

    	}

    	return super.onOptionsItemSelected(item);
    }

}
